package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerGroupMerchant;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerGroupMerchantMapper;
import com.xydtech.ecard.ecard.domain.EcardApiEnterpriseInfo;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantTask;
import com.xydtech.ecard.ecard.mapper.EcardApiEnterpriseInfoMapper;
import com.xydtech.ecard.ecard.mapper.EcardApiMerchantTaskMapper;
import com.xydtech.ecard.ecard.service.IEcardApiEnterpriseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片企业信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-07-18 下午4:44
 */
@Service
public class EcardApiEnterpriseInfoServiceImpl implements IEcardApiEnterpriseInfoService {

    @Autowired
    private EcardApiEnterpriseInfoMapper ecardEnterpriseInfoMapper;
    @Autowired
    private EcardApiMerchantTaskMapper ecardMerchantTaskMapper;
    @Autowired
    private EcardApiCustomerGroupMerchantMapper ecardCustomerGroupMerchantMapper;

    /**
     * 查询电子名片我的企业信息列表
     * @param ecardEnterpriseInfo 电子名片企业信息
     * @return
     */
    @Override
    public List<EcardApiEnterpriseInfo> getMyEnterpriseList(EcardApiEnterpriseInfo ecardEnterpriseInfo,String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardEnterpriseInfo.getContactsPhone())) {
                ecardEnterpriseInfo.setContactsPhone(AESEncryptionUtils.encrypt(ecardEnterpriseInfo.getContactsPhone(), salt));
            }
            List<EcardApiEnterpriseInfo> enterpriseList = ecardEnterpriseInfoMapper.getMyEnterpriseList(ecardEnterpriseInfo);
            if (enterpriseList.size() > 0) {
                for (EcardApiEnterpriseInfo enterpriseInfo : enterpriseList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(enterpriseInfo.getContactsPhone())) {
                        enterpriseInfo.setContactsPhone(AESEncryptionUtils.decrypt(enterpriseInfo.getContactsPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(enterpriseInfo.getEntPhoneNumber())) {
                        enterpriseInfo.setEntPhoneNumber(AESEncryptionUtils.decrypt(enterpriseInfo.getEntPhoneNumber(), salt));
                    }
                }
            }
            return enterpriseList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 唯一值验证,相同行业、支行、门头名不能重复
     * @param ecardEnterpriseInfo
     * @return
     */
    @Override
    public int validateEnterpriseInfo(EcardApiEnterpriseInfo ecardEnterpriseInfo) {
        return ecardEnterpriseInfoMapper.validateEnterpriseInfo(ecardEnterpriseInfo);
    }

    /**
     * 查询电子名片企业信息
     * @param id
     * @return
     */
    @Override
    public EcardApiEnterpriseInfo selectEcardEnterpriseInfoById(Long id, String salt) {
        EcardApiEnterpriseInfo enterpriseInfo = ecardEnterpriseInfoMapper.selectEcardEnterpriseInfoById(id);
        try {
            if (enterpriseInfo != null) {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(enterpriseInfo.getEntPhoneNumber())) {
                    enterpriseInfo.setEntPhoneNumber(AESEncryptionUtils.decrypt(enterpriseInfo.getEntPhoneNumber(), salt));
                }
            }
            return enterpriseInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询电子名片企业信息列表
     * @param ecardEnterpriseInfo
     * @return
     */
    @Override
    public List<EcardApiEnterpriseInfo> getEnterpriseList(EcardApiEnterpriseInfo ecardEnterpriseInfo) {
        return ecardEnterpriseInfoMapper.getEnterpriseList(ecardEnterpriseInfo);
    }

    /**
     * 保存电子名片企业信息列表
     * @param ecardEnterpriseInfo
     * @return
     */
    @Override
    public int addEnterpriseInfo(EcardApiEnterpriseInfo ecardEnterpriseInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardEnterpriseInfo.getEntPhoneNumber())) {
                ecardEnterpriseInfo.setEntPhoneNumber(AESEncryptionUtils.encrypt(ecardEnterpriseInfo.getEntPhoneNumber(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardEnterpriseInfoMapper.addEnterpriseInfo(ecardEnterpriseInfo);
    }

    /**
     * 查询企业地图坐标列表接口
     * @param creatorId
     * @param deptId
     * @param industry
     * @return
     */
    @Override
    public List<EcardApiEnterpriseInfo> getEnterpriseCoordinateList(Long creatorId, Long deptId, Integer industry, String entLocationType) {
        return ecardEnterpriseInfoMapper.getEnterpriseCoordinateList(creatorId, deptId, industry, entLocationType);
    }

    /**
     * 根据法人机构ID查询机构下所有企业坐标信息列表
     * @param deptId
     * @param industry
     * @return
     */
    @Override
    public List<EcardApiEnterpriseInfo> getAllEnterpriseCoordinateList(Long creatorId, Long deptId, Integer industry, String entLocationType) {
        return ecardEnterpriseInfoMapper.getAllEnterpriseCoordinateList(creatorId, deptId, industry, entLocationType);
    }

    /**
     * 查询电子名片企业信息
     * @param id
     * @param creatorId
     * @return
     */
    @Override
    public EcardApiEnterpriseInfo selectEcardEnterpriseInfoByIdAndCreatorId(Long id, Long creatorId) {
        return ecardEnterpriseInfoMapper.selectEcardEnterpriseInfoByIdAndCreatorId(id, creatorId);
    }

    /**
     * 根据ID修改企业状态
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeEnterpriseStatus(Long id) {
        //查询是否存在未处理营销任务
        EcardApiMerchantTask merchantTask = ecardMerchantTaskMapper.getMerchantTaskByMerchantId(id, "3");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        if (merchantTask!=null) {
            //存在则更新为已完成
            merchantTask.setTaskSts("1");
            ecardMerchantTaskMapper.updateEcardMerchantTask(merchantTask);
        }
        //同步修改行业客户中的状态
        EcardApiCustomerGroupMerchant groupMerchant = new EcardApiCustomerGroupMerchant();
        groupMerchant.setMerchantId(id);
        groupMerchant.setMerchantType("3");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        List<EcardApiCustomerGroupMerchant> customerGroupMerchantList = ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantList(groupMerchant);
        if (customerGroupMerchantList.size() > 0) {
            ecardCustomerGroupMerchantMapper.changeIndustrialCustomerStatus(customerGroupMerchantList.get(0).getId());
        }
        return ecardEnterpriseInfoMapper.changeEnterpriseStatus(id);
    }
}
